from fastapi import FastAPI
from starlette.staticfiles import StaticFiles

# 全局异常
from app.core.exceptions import register_exception_handlers

# 全局中间件
from app.core.middleware import ApiLogginMiddleware

# 生命周期
from app.core.app_init import lifespan


# 临时解决跨域
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None)

# 静态文件路径
app.mount("/static", StaticFiles(directory="static", check_dir=False), name="static")

# 1. 注册全局异常处理
register_exception_handlers(app)
# 2. 全局中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
app.add_middleware(ApiLogginMiddleware)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run("main:app", host="0.0.0.0", port=9001, reload=False)
